home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Is this a memory leak?
- Date: Fri, 05 Apr 1996 12:04:01 -0500
- Organization: Datalytics, Inc
- Message-ID: <31655281.78CB@datalytics.com>
- References: <4jv214$gv7@insosf1.netins.net> <4k0c2i$h6e@werple.net.au>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- David White wrote:
- >
- > hhowe@trgnet.com (Harold Howe) writes:
- >
- > >Could someone please tell me if this code leaks memory
- >
- > >class BuriedClass
- > > {
- > > ...
- > > }
- >
- > >class TopClass
- > > {
- > > private
- > > BuriedClass *bury;
- > > public:
- > > TopClass::TopClass() { bury = new BuriedClass();}
- > > shutDown { bury = 0;}
- > > ~TopClass { delete bury}
- > > }
- >
- > >int main(void)
- > > {
- > > TopClass *top = new TopClass();
- > > top->shutDown();
- > > delete top;
- > > return 0;
- > > }
- > [snip]
- > The BuriedClass object is not deleted. Zeroing the pointer does nothing
- > but zero the pointer, which prevents the BuriedClass object from being
- > deleted. Maybe 'shutDown' is intended to be called if responsibility for
- > the BuriedClass object is moved to something else, which will delete it,
- > or maybe it's a safety measure, to be used if some sort of free store
- > corruption is detected.
- >
-
- Possibly, shutDown was supposed to delete the memory before
- setting the pointer to zero.
-
- There is also another question. Why is top allocated on the
- heap? I realize this is a short excerpt of code to illustrate
- something else, but this is a common thing. You should avoid
- the heap whenever possible. A heap allocation involves fairly
- lengthy operations, while a stack allocation involves a
- subtraction. The result is higher performance.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-